using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP.Commons;
using WP.DB;
using WP.Modules;

namespace MaxLeaf
{
    public partial class frmAlertOption : BaseQryRpt
    {
        public frmAlertOption()
        {
            InitializeComponent();
        }

        private bool SaveOption()
        {
            bool bOptionsWriteToFile = true;
            try
            {

                DBAccess dbcon = new DBAccess(false, false);

                string strSql = "Delete from mUserDefault where userName='" + SysVar.UserName + "' and ProgName='" + this.Name  + "' and  Parameter='0'";
                dbcon.ExecuteSql(strSql);

                strSql = "Insert into mUserDefault (UserName, ProgName,Parameter,Name,DefaultString) values ('" +
                    SysVar.UserName + "','" + this.Name + "','" + "0" + "','" + chkUserAlert.Name +"','" + (chkUserAlert.Checked ? "1" : "0") + "')";
                dbcon.ExecuteSql(strSql);

                strSql = "Insert into mUserDefault (UserName, ProgName,Parameter,Name,DefaultString) values ('" +
                   SysVar.UserName + "','" + this.Name + "','" + "0" + "','" + btnFormColor.Name + "','" + this.btnFormColor.BackColor.ToArgb().ToString("x") + "')";
                dbcon.ExecuteSql(strSql);
                strSql = "Insert into mUserDefault (UserName, ProgName,Parameter,Name,DefaultString) values ('" +
                      SysVar.UserName + "','" + this.Name + "','" + "0" + "','" + btnGridColor.Name + "','" + this.btnGridColor.BackColor.ToArgb().ToString("x") + "')";
                dbcon.ExecuteSql(strSql);
                strSql = "Insert into mUserDefault (UserName, ProgName,Parameter,Name,DefaultString) values ('" +
                      SysVar.UserName + "','" + this.Name + "','" + "0" + "','" + txtAlertRefreshTime.Name + "','" + txtAlertRefreshTime.Text   + "')";
                dbcon.ExecuteSql(strSql);

                strSql = "Insert into mUserDefault (UserName, ProgName,Parameter,Name,DefaultString) values ('" +
                      SysVar.UserName + "','" + this.Name + "','" + "0" + "','" + btnMsgWinBackColor.Name + "','" + this.btnMsgWinBackColor.BackColor.ToArgb().ToString("x") + "')";
                dbcon.ExecuteSql(strSql);

                strSql = "Insert into mUserDefault (UserName, ProgName,Parameter,Name,DefaultString) values ('" +
                      SysVar.UserName + "','" + this.Name + "','" + "0" + "','" + txtAlertWaitTime.Name + "','" + txtAlertWaitTime.Text + "')";
                dbcon.ExecuteSql(strSql);


                dbcon.CloseCon();
            }
            catch (Exception ex)
            {
                bOptionsWriteToFile = false;
                ExceptionManager.ProcessException(ex);
            }

            return bOptionsWriteToFile;
        }


        private void LoadOption()
        {
            DBAccess dbcon = new DBAccess(false, false);
            DataView dv = dbcon.GetDataTable("select * from  mUserDefault where userName='" + SysVar.UserName + "' and ProgName='" + this.Name + "'").DefaultView;
            dv.RowFilter = "Name='" + chkUserAlert.Name + "'";
            if (dv.Count == 1) chkUserAlert.Checked = CvtFunc.ToString(dv[0]["DefaultString"]).Trim() == "1" ? true : false;

            dv.RowFilter = "Name='" + btnFormColor.Name + "'";
            if (dv.Count == 1) btnFormColor.BackColor = System.Drawing.Color.FromArgb(int.Parse((CvtFunc.ToString(dv[0]["DefaultString"]).Trim() == "" ? "0" : CvtFunc.ToString(dv[0]["DefaultString"]).Trim()), System.Globalization.NumberStyles.AllowHexSpecifier));

            dv.RowFilter = "Name='" + btnGridColor.Name + "'";
            if (dv.Count == 1)  btnGridColor.BackColor = System.Drawing.Color.FromArgb(int.Parse((CvtFunc.ToString(dv[0]["DefaultString"]).Trim() == "" ? "0" : CvtFunc.ToString(dv[0]["DefaultString"]).Trim()), System.Globalization.NumberStyles.AllowHexSpecifier));

            dv.RowFilter = "Name='" + txtAlertRefreshTime.Name + "'";
            if (dv.Count == 1 && CvtFunc.ToInt(dv[0]["DefaultString"]) > 0)
                txtAlertRefreshTime.Text = CvtFunc.ToInt(dv[0]["DefaultString"]).ToString().Trim();
            else
                txtAlertRefreshTime.Text = "30";

            dv.RowFilter = "Name='" + txtAlertWaitTime.Name + "'";
            if (dv.Count == 1 && CvtFunc.ToInt(dv[0]["DefaultString"]) > 0)
                txtAlertWaitTime.Text = CvtFunc.ToInt(dv[0]["DefaultString"]).ToString().Trim();
            else
                txtAlertWaitTime.Text = "10";

            dv.RowFilter = "Name='" + btnMsgWinBackColor.Name + "'";
            if (dv.Count == 1)  btnMsgWinBackColor.BackColor = System.Drawing.Color.FromArgb(int.Parse((CvtFunc.ToString(dv[0]["DefaultString"]).Trim() == "" ? "0" : CvtFunc.ToString(dv[0]["DefaultString"]).Trim()), System.Globalization.NumberStyles.AllowHexSpecifier));
            
            dbcon.CloseCon();  

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!SaveOption()) return;
            this.Close(); 

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void frmAlertOption_Load(object sender, EventArgs e)
        {
            LoadOption(); 
        }

        private void btnFormColor_Click(object sender, EventArgs e)
        {
            if (this.colorDialog.ShowDialog() == DialogResult.OK)
            {
                this.btnFormColor.BackColor = this.colorDialog.Color;
            }
        }

        private void btnGridColor_Click(object sender, EventArgs e)
        {
            if (this.colorDialog.ShowDialog() == DialogResult.OK)
            {
                this.btnGridColor.BackColor = this.colorDialog.Color;
            }
        }

        private void btnMsgWinBackColor_Click(object sender, EventArgs e)
        {
            if (this.colorDialog.ShowDialog() == DialogResult.OK)
            {
                this.btnMsgWinBackColor.BackColor = this.colorDialog.Color;
            }
        }

        private void txtAlertRefreshTime_TextChanged(object sender, EventArgs e)
        {
            if (CvtFunc.ToInt(txtAlertRefreshTime.Text) > 0) chkUserAlert.Checked = true; 
 
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}